package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpProductPriceControl;
import com.erp.gather.domain.bo.ErpProductPriceControlBo;
import com.erp.gather.domain.vo.ErpProductPriceControlVo;
import com.erp.gather.mapper.ErpProductPriceControlMapper;
import com.erp.gather.service.IErpProductPriceControlService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品价格调控Service业务层处理
 *
 * @author erp
 * @date 2023-12-07
 */
@RequiredArgsConstructor
@Service
public class ErpProductPriceControlServiceImpl implements IErpProductPriceControlService {

    private final ErpProductPriceControlMapper baseMapper;

    /**
     * 查询商品价格调控
     */
    @Override
    public ErpProductPriceControlVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品价格调控列表
     */
    @Override
    public TableDataInfo<ErpProductPriceControlVo> queryPageList(ErpProductPriceControlBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpProductPriceControl> lqw = buildQueryWrapper(bo);
        Page<ErpProductPriceControlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品价格调控列表
     */
    @Override
    public List<ErpProductPriceControlVo> queryList(ErpProductPriceControlBo bo) {
        LambdaQueryWrapper<ErpProductPriceControl> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpProductPriceControl> buildQueryWrapper(ErpProductPriceControlBo bo) {
        LambdaQueryWrapper<ErpProductPriceControl> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getControlType()), ErpProductPriceControl::getControlType, bo.getControlType());
        lqw.eq(StringUtils.isNotBlank(bo.getControlWay()), ErpProductPriceControl::getControlWay, bo.getControlWay());
        lqw.eq(StringUtils.isNotBlank(bo.getPriceType()), ErpProductPriceControl::getPriceType, bo.getPriceType());
        lqw.eq(StringUtils.isNotBlank(bo.getAmount()), ErpProductPriceControl::getAmount, bo.getAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getCountry()), ErpProductPriceControl::getCountry, bo.getCountry());
        return lqw;
    }

    /**
     * 新增商品价格调控
     */
    @Override
    public Boolean insertByBo(ErpProductPriceControlBo bo) {
        ErpProductPriceControl add = BeanUtil.toBean(bo, ErpProductPriceControl.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品价格调控
     */
    @Override
    public Boolean updateByBo(ErpProductPriceControlBo bo) {
        ErpProductPriceControl update = BeanUtil.toBean(bo, ErpProductPriceControl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpProductPriceControl entity){
        //TODO 做一些数据校验,如唯一约束
    	
    	// 查找是否有
    	LambdaQueryWrapper<ErpProductPriceControl> lqw = Wrappers.lambdaQuery();
    	lqw.eq(ErpProductPriceControl::getControlType, entity.getControlType()); // 调价类型
    	lqw.eq(ErpProductPriceControl::getCountry, entity.getCountry());
    	lqw.ne(entity.getId() != null, ErpProductPriceControl::getId, entity.getId());
    	
    	ErpProductPriceControl control = baseMapper.selectOne(lqw);
    	if(control != null) {
    		throw new RuntimeException("该国家已有该类型的调价策略，请勿重复设置");
    	}
    }

    /**
     * 批量删除商品价格调控
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
    
    /**
     * 根据类型获取调价策略集合
     * @return
     */
    @Override
    public Map<String, ErpProductPriceControl> getListByType(String controlType) {
    	LambdaQueryWrapper<ErpProductPriceControl> lqw = Wrappers.lambdaQuery();
    	lqw.eq(ErpProductPriceControl::getControlType, controlType);
    	List<ErpProductPriceControl> list = baseMapper.selectList(lqw);
    	
    	Map<String, ErpProductPriceControl> map = new HashMap<String, ErpProductPriceControl>();
    	for (ErpProductPriceControl erpProductPriceControl : list) {
    		map.put(erpProductPriceControl.getCountry(), erpProductPriceControl);
		}
    	
    	return map;
    }
}
